package com.jzy.gulimall.member.config;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jzy
 * @date 2020/9/20 17:28
 * feign远程调用丢失请求头
 */
@Configuration
public class GulimallFeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor(){
        return template -> {
            System.out.println("begin->feign拦截器");
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes != null) {
                //同步请求头数据
                HttpServletRequest request = attributes.getRequest();
                if (request != null) {
                    template.header("Cookie", request.getHeader("Cookie"));
                }
            }
        };
    }

}
